package cn.digiwin.wx.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Title:加密，解密类，不能直接加密中文，中文要urlencode一下再加密
 * <p>
 * Description:
 * <p>
 * Copyright: Copyright (c) 2005
 *
 * @author greenli
 * @version $Id: SecurityUtil.java,v 1.4 2013/05/21 08:34:16 dongxx Exp $
 * @created 2005-6-7 11:04:33
 */
public class SecurityUtil {

    private static final String key = "AEBFADEBA9ABCDEF";

    private static final String iv = "12345679";

    private static final SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "Blowfish");

    private static final IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

    public static String encrypt(int istr) {
        return encrypt(istr + "");
    }

    public static String encrypt(String str) {
        if (str != null) {
            String blank = "            ";
            str = blank + str + blank + blank.substring(0, (8 - str.length() % 8) % 8);
            byte[] encrypted;
            String re = null;
            try {
                Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
                cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
                encrypted = cipher.doFinal(str.getBytes());
                re = bytesToHex(encrypted);
            } catch (Exception e) {
                System.err.println("Encrypted error: " + str);
                e.printStackTrace();
            }
            return re;
        } else {
            return str;
        }
    }

    /**
     * 解密
     *
     * @param str
     * @return
     */
//    public static String decrypt(String str) {
//        if (StringUtils.isEmpty(str)) {
//            return null;
//        }
//        try {
//            Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
//            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
//            byte[] decrypted = cipher.doFinal(Hex.decode(str));
//            String d = new String(decrypted);
//            return StringUtils.trim(d);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//    }

    public static String md5(String inputText) {
        if (inputText == null || "".equals(inputText.trim())) {
            throw new IllegalArgumentException("请输入要加密的内容");
        }

        String encryptText = null;
        try {
            MessageDigest m = MessageDigest.getInstance("md5");
            m.update(inputText.getBytes("gbk"));
            byte s[] = m.digest();
            return bytesToHex(s);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encryptText;
    }

    public static String bytesToHex(byte[] data) {
        if (data == null) {
            return null;
        } else {
            int len = data.length;
            String str = "";
            for (int i = 0; i < len; i++) {
                if ((data[i] & 0xFF) < 16)
                    str = str + "0" + java.lang.Integer.toHexString(data[i] & 0xFF);
                else
                    str = str + java.lang.Integer.toHexString(data[i] & 0xFF);

            }
            return str;
        }

    }

    //sha1加密
    public static String sHA1Encode(String sourceString) {
        String resultString = null;
        try {
            resultString = new String(sourceString);
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            resultString = byte2hexString(md.digest(resultString.getBytes()));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return resultString;
    }

    private static String byte2hexString(byte[] bytes) {
        StringBuffer buf = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            if (((int) bytes[i] & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toString((int) bytes[i] & 0xff, 16));
        }
        return buf.toString().toUpperCase();
    }

    public static void main(String[] args) throws Exception {
        String src = args[0];

        // String e = encrypt(src);
        // System.err.println("eeee===" + e);
//        for (int i = 0; i < 100; i++) {
//            System.err.println("ssss===" + decrypt(args[0]));
//        }

        // System.err.println("ssss===" + oldDecrypt(src));
    }

}
